package com.chencs.common.role.controller;

import com.alibaba.fastjson.JSON;
import com.chencs.common.role.service.RoleService;
import com.chencs.vo.common.grant.RoleModel;
import framework.model.ResponseOptUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by fangzehua on 2017/6/11.
 */

@RequestMapping("/role")
@RestController
public class RoleController {

    private static final Logger logger = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    RoleService roleService;

    @RequestMapping(value = "/roleList", method = RequestMethod.GET)
        public ModelAndView main() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/common/role/role_list");
        return modelAndView;
    }

    @RequestMapping(value = "/doRolePage", method = RequestMethod.GET)
    public ModelAndView doRolePage() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/common/role/role_add");
        return modelAndView;
    }

    @RequestMapping(value = "/doEditor/{id}", method = RequestMethod.GET)
    public ModelAndView doRolePage(@PathVariable("id") int id ,Model  model) {
        RoleModel roleModel = null;
        try{
            roleModel = JSON.parseObject(roleService.findRoleById(id+""),RoleModel.class);
       }catch (Exception e){
           e.printStackTrace();
       }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/common/role/role_update");
        model.addAttribute("roleModel",roleModel);
        return modelAndView;
    }

    @RequestMapping(value = "/addRole", method = RequestMethod.POST)
    public String addRole(@RequestBody RoleModel model) {
        logger.info("/role/addRole param: {}", JSON.toJSON(model) );
        String retStr = "";
        try {
            retStr = roleService.addRole(model);
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError( ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list() {
        logger.info("/role/list param: {}", "");
        String retStr = "";
        try {
            retStr = roleService.getList();
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError( ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public String delete(@RequestBody RoleModel model) {
        logger.info("/role/delete param: {}", JSON.toJSON(model));
        String retStr = "";
        try {
            retStr = roleService.delete(model);
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError( ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(@RequestBody RoleModel model) {
        logger.info("/role/update param: {}", JSON.toJSON(model));
        String retStr = "";
        try {
            retStr = roleService.update(model);
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError( ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;
    }




}
